import {Component, ElementRef, OnInit} from '@angular/core';
import {ActivatedRoute, Router} from "@angular/router";
import {DialogService} from "../../service/dialogService";
import {CCRiskControllerService} from "../../service/CCRiskController";
import {UserService} from "../../service/userService";
import {BaseAuthenticateComponent} from "../../shared/component/baseAuthenticate.component";

@Component({
  selector: 'app-about-me',
  templateUrl: './about-me.component.html',
  styleUrls: ['./about-me.component.css']
})
export class AboutMeComponent extends BaseAuthenticateComponent {

  constructor(protected a_router: Router, public element: ElementRef, public userService: UserService,
              public route: ActivatedRoute, public _dialogService: DialogService, public riskService: CCRiskControllerService) {
    super(a_router, element, route)
  }

  ngOnInit() {
  }

  logout() {
    this.a_router.navigate(["/login"]);
  }

  unbind() {
    if (this.loginUser) {
      this._dialogService.pushDialogMsg({
        showNOButton: true,
        title: "解除微信用户绑定",
        content: "您确定要解除微信用户绑定吗？",
        btnNOText: "取消",
        btnOKText: "解绑"
      }, () => {
        this.userService.unregisterWeChatUser({userId: this.loginUser.userid, openId: this.openid}).subscribe(
          () => {
            // 保留微信用户信息
            localStorage.removeItem("loginUser");
            localStorage.removeItem("token");
            localStorage.removeItem("openid");
            this._dialogService.pushDialogMsg({showNOButton: false, content: "成功解除微信用户绑定", btnOKText: "知道了"});
            this.a_router.navigate(['/scan']);
          },
          (err) => {
            this.handleError(err);
          }
        );
      });
    }
  }

}
